//
//  MainWindowController.m
//  ScMix
//
//  Created by apple on 18/10/11.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MainWindowController.h"
#import <Masonry.h>
#import "BaseNSView.h"
#import "OptionsView.h"
#import "ConsoleView.h"




@interface MainWindowController ()

@end

@implementation MainWindowController


- (instancetype)initWithWindow:(NSWindow *)window{
    if (self = [super initWithWindow:window]) {
        [self loadWindows];
    }
    return self;
}


- (void)windowDidLoad {
    [super windowDidLoad];
    
    
    [self.window setFrame:NSMakeRect(0, 0, self.window.screen.visibleFrame.size.width, self.window.screen.visibleFrame.size.height + 20) display:YES];
    [self.window setTitlebarAppearsTransparent:YES];
    [self.window setMinSize:CGSizeMake(500, self.window.screen.visibleFrame.size.height)];
    [self.window setMaxSize:CGSizeMake(self.window.screen.visibleFrame.size.width, self.window.screen.visibleFrame.size.height)];
    [self.window setStyleMask:(self.window.styleMask | NSFullSizeContentViewWindowMask)];
    self.window.contentView.layer.backgroundColor = [NSColor clearColor].CGColor;
    [self loadWindows];
    

}


- (void)loadWindows{
    CGFloat toolbarHeight = 40;

    //整体split
    NSSplitView *splitView = [[NSSplitView alloc] initWithFrame:NSMakeRect(0, 0,self.window.contentView.frame.size.width, self.window.contentView.frame.size.height - toolbarHeight)];
    [self.window.contentView addSubview:splitView];
    splitView.wantsLayer = YES;
    splitView.layer.backgroundColor = [NSColor whiteColor].CGColor;
    //分割方向:垂直/水平
    [splitView setVertical:YES];
    //分割区域的样式
    [splitView setDividerStyle:NSSplitViewDividerStylePaneSplitter];
    
    
    //log窗口
    NSView *logView = [[BaseNSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 0)];
    logView.autoresizingMask = NO;
    CALayer *logViewLayer = [CALayer layer];
    [logViewLayer setBackgroundColor:CGColorCreateGenericRGB(239/255.0, 241/255.0, 243/255.0, 1)];
    [logView setWantsLayer:YES];
    [logView setLayer:logViewLayer];
    [logView setAutoresizesSubviews:YES];
    
    //中间窗口
    NSView *centerView = [[BaseNSView alloc] initWithFrame:NSMakeRect(0, 0, 400, 0)];
    centerView.autoresizingMask = NO;
    [centerView setAutoresizesSubviews:YES];
    
    //中间split窗口
    NSSplitView *splitViewCenter = [[NSSplitView alloc] initWithFrame:NSZeroRect];
    [splitViewCenter setVertical:NO];
    [splitViewCenter setDividerStyle:NSSplitViewDividerStyleThin];
    splitViewCenter.wantsLayer = YES;
    splitViewCenter.layer.backgroundColor = [NSColor whiteColor].CGColor;
    [centerView addSubview:splitViewCenter];
    [splitViewCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(centerView);
    }];
    
    //中间混淆选项窗口
//    
//    OptionsView *optionsView = [[OptionsView alloc] initWithFrame:NSMakeRect(0, 0, 100, 5)];
//    optionsView.autoresizingMask = NO;
//    CALayer *optionsViewLayer = [CALayer layer];
//    [optionsViewLayer setBackgroundColor:CGColorCreateGenericRGB(241/255.0, 241/255.0, 241/255.0, 1)];
//    [optionsView setWantsLayer:YES];
//    [optionsView setLayer:optionsViewLayer];
//    [optionsView setAutoresizesSubviews:YES];
    
    NSView *optionsView = nil;
    NSNib *xib = [[NSNib alloc] initWithNibNamed:@"OptionsView" bundle:nil];
    NSArray *viewsArray = [[NSArray alloc] init];
    [xib instantiateWithOwner:nil topLevelObjects:&viewsArray];
    for (int i = 0; i < viewsArray.count; i++) {
        if ([viewsArray[i] isKindOfClass:[NSView class]]) {
            optionsView = (NSView *)viewsArray[i];
            break;
        }
    }
  
    optionsView.frame = CGRectMake(0, 0, 100, 5);
    CALayer *optionsViewLayer = [CALayer layer];
    [optionsViewLayer setBackgroundColor:CGColorCreateGenericRGB(241/255.0, 241/255.0, 241/255.0, 1)];
    [optionsView setWantsLayer:YES];
    [optionsView setLayer:optionsViewLayer];
    [optionsView setAutoresizesSubviews:YES];
    
    //中间console进度窗口
    ConsoleView *consoleView = [[ConsoleView alloc] initWithFrame:NSMakeRect(0, 0, 0, 2)];
    consoleView.autoresizingMask = NO;
    [(OptionsView *)optionsView setDelegate:consoleView];
    CALayer *consoleViewLayer = [CALayer layer];
    [consoleViewLayer setBackgroundColor:CGColorCreateGenericRGB(241/255.0, 241/255.0, 241/255.0, 1)];
    [consoleView setWantsLayer:YES];
    [consoleView setLayer:consoleViewLayer];
    
    [splitViewCenter addSubview:optionsView];
    [splitViewCenter addSubview:consoleView];
    
    //添加view
    [splitView addSubview:logView];
    [splitView addSubview:centerView];
    [splitView setAutoresizesSubviews:YES];
    [splitView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable ];

    
    
    
    
    
}



@end
